# 医疗病历评估系统 - 启动脚本
# 使用方法：在 PowerShell 中运行 .\start.ps1

Write-Host "=" -NoNewline -ForegroundColor Cyan
Write-Host "=" * 79 -ForegroundColor Cyan
Write-Host "🚀 医疗病历评估系统启动脚本" -ForegroundColor Green
Write-Host "=" -NoNewline -ForegroundColor Cyan
Write-Host "=" * 79 -ForegroundColor Cyan
Write-Host ""

# 步骤 1: 检查 Ollama
Write-Host "📋 步骤 1/5: 检查 Ollama 服务..." -ForegroundColor Yellow
try {
    $ollamaStatus = Invoke-WebRequest -Uri "http://localhost:11434/api/tags" -TimeoutSec 2 -ErrorAction SilentlyContinue
    Write-Host "   ✅ Ollama 服务运行中" -ForegroundColor Green
} catch {
    Write-Host "   ❌ Ollama 服务未运行" -ForegroundColor Red
    Write-Host "   请在新终端运行: ollama serve" -ForegroundColor Yellow
    Write-Host ""
    Read-Host "按 Enter 继续（如果 Ollama 已在其他终端运行）"
}

# 步骤 2: 检查 Python
Write-Host ""
Write-Host "📋 步骤 2/5: 检查 Python..." -ForegroundColor Yellow
$pythonVersion = python --version 2>&1
Write-Host "   ✅ $pythonVersion" -ForegroundColor Green

# 步骤 3: 检查 GPU
Write-Host ""
Write-Host "📋 步骤 3/5: 检查 GPU..." -ForegroundColor Yellow
try {
    $gpuInfo = nvidia-smi --query-gpu=name,memory.total --format=csv,noheader 2>&1
    if ($LASTEXITCODE -eq 0) {
        Write-Host "   ✅ GPU: $gpuInfo" -ForegroundColor Green
    } else {
        Write-Host "   ⚠️  未检测到 NVIDIA GPU，将使用 CPU（速度较慢）" -ForegroundColor Yellow
    }
} catch {
    Write-Host "   ⚠️  未检测到 NVIDIA GPU" -ForegroundColor Yellow
}

# 步骤 4: 检查依赖
Write-Host ""
Write-Host "📋 步骤 4/5: 检查 Python 依赖..." -ForegroundColor Yellow
$packages = @("transformers", "torch", "fastapi", "sqlalchemy")
foreach ($package in $packages) {
    try {
        python -c "import $package" 2>$null
        if ($LASTEXITCODE -eq 0) {
            Write-Host "   ✅ $package 已安装" -ForegroundColor Green
        } else {
            Write-Host "   ❌ $package 未安装" -ForegroundColor Red
        }
    } catch {
        Write-Host "   ❌ $package 未安装" -ForegroundColor Red
    }
}

# 步骤 5: 启动后端
Write-Host ""
Write-Host "📋 步骤 5/5: 启动后端服务..." -ForegroundColor Yellow
Write-Host ""
Write-Host "=" -NoNewline -ForegroundColor Cyan
Write-Host "=" * 79 -ForegroundColor Cyan
Write-Host "⚡ 正在启动后端服务..." -ForegroundColor Green
Write-Host "=" -NoNewline -ForegroundColor Cyan
Write-Host "=" * 79 -ForegroundColor Cyan
Write-Host ""

# 切换到后端目录
Set-Location -Path "src\backend"

# 启动服务
python -m app.main
